package cn.group7.padserver.controller;

import cn.group7.padserver.common.Flag;
import cn.group7.padserver.controller.responseformat.Code;
import cn.group7.padserver.controller.responseformat.Result;
import cn.group7.padserver.entity.PasswordParam;
import cn.group7.padserver.pojo.BankUser;
import cn.group7.padserver.service.BankUserService;
import cn.group7.padserver.utils.JwtTokenUtil;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author 谷汉斌
 * @description
 * @createTime 2024/8/4 下午9:50
 */
@RestController
@RequestMapping("/bankusers")
public class BankUserController {

    @Autowired
    private BankUserService bankUserService;


    @Resource
    private UserDetailsService jwtUserDetailsService;

    @Resource
    private JwtTokenUtil jwtTokenUtil;


    /**
     * 用户登录之后，修改用户密码
     */
    @ApiOperation(value = "用户登录之后，修改用户密码")
    @PostMapping("/loginUpdatePassword")
    public Result loginUpdatePassword(@Validated @RequestBody PasswordParam passwordParam) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserDetails principal = (UserDetails) authentication.getPrincipal();
        String username = principal.getUsername();
        BankUser bankUser = bankUserService.selectByUserName(username);
        //数据库密码
        String oldDataBasePassword = bankUser.getPassword();
        //传过来的老密码
        String oldPassword = passwordParam.getOldPassword();
        if (!new BCryptPasswordEncoder().matches(oldPassword, oldDataBasePassword)) {
            return Result.builder().flag(Flag.FAIL.isCode()).code(Code.UPDATE_FAIL.getCode()).message(Code.UPDATE_FAIL.getInfo()).build();
        }
        bankUser.setUsername(username);
        bankUser.setPassword(passwordParam.getNewPassword());
        bankUserService.update(bankUser);
        return Result.builder().flag(Flag.SUCCESS.isCode())
                .code(Code.UPDATE_SUCCESS.getCode())
                .message(Code.UPDATE_SUCCESS.getInfo()).build();
    }


    /**
     * 用户登录之后，根据用户名展示个人信息
     */
    @ApiOperation(value = "用户登录之后，根据用户名展示个人信息")
    @GetMapping("/loginSelectByUsername")
    public Result loginSelectByUsername() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserDetails principal = (UserDetails) authentication.getPrincipal();
        String username = principal.getUsername();
        BankUser bankUser = bankUserService.selectByUserName(username);
        return Result.builder().flag(Flag.SUCCESS.isCode()).code(Code.QUERY_SUCCESS.getCode())
                .message(Code.QUERY_SUCCESS.getInfo()).data(bankUser).build();
    }

    /**
     * 用户登录之后，根据用户名修改个人基本信息
     */
    @ApiOperation(value = "用户登录之后，根据用户名修改个人基本信息")
    @PostMapping("/loginUpdateByUsername")
    public Result loginUpdateByUsername(@Validated @RequestBody BankUser bankUser) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserDetails principal = (UserDetails) authentication.getPrincipal();
        String username = principal.getUsername();
        bankUser.setUsername(username);
        boolean update = bankUserService.update(bankUser);
        UserDetails userDetails = jwtUserDetailsService.loadUserByUsername(username);
        String token = jwtTokenUtil.generateToken(userDetails);
        return Result.builder().code(update ? Code.UPDATE_SUCCESS.getCode() : Code.UPDATE_FAIL.getCode())
                .data(update?token:null)
                .message(update ? Code.UPDATE_SUCCESS.getInfo() : Code.UPDATE_FAIL.getInfo())
                .flag(update ? Flag.SUCCESS.isCode() : Flag.FAIL.isCode())
                .build();
    }
}
